package com.kjsdfgk.hjmdsf.ftvdolist.mvp.ui.fragment;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.jess.arms.base.BaseFragment;
import com.jess.arms.di.component.AppComponent;
import com.jess.arms.utils.ArmsUtils;

import com.kjsdfgk.hjmdsf.app.entity.Account;
import com.kjsdfgk.hjmdsf.app.entity.ListBean;
import com.kjsdfgk.hjmdsf.app.entity.XJVideoJson;
import com.kjsdfgk.hjmdsf.app.msg.AddViewMsg;
import com.kjsdfgk.hjmdsf.app.utils.SharedPrefsUtils;
import com.kjsdfgk.hjmdsf.charge.mvp.ui.activity.MemberChargeActivity;
import com.kjsdfgk.hjmdsf.ftvdolist.di.component.DaggerVdoListComponent;
import com.kjsdfgk.hjmdsf.ftvdolist.mvp.contract.VdoListContract;
import com.kjsdfgk.hjmdsf.ftvdolist.mvp.presenter.VdoListPresenter;
import com.kjsdfgk.hjmdsf.play.mvp.ui.activity.MyPlayVideoActivity;
import com.lksdkjs.live.R;
import com.qmuiteam.qmui.widget.dialog.QMUIDialog;


import org.greenrobot.eventbus.EventBus;
import org.litepal.LitePal;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

import static com.jess.arms.utils.Preconditions.checkNotNull;


/**
 * ================================================
 * Description:
 * <p>
 * Created by MVPArmsTemplate on 04/30/2020 18:02
 * <a href="mailto:jess.yan.effort@gmail.com">Contact me</a>
 * <a href="https://github.com/JessYanCoding">Follow me</a>
 * <a href="https://github.com/JessYanCoding/MVPArms">Star me</a>
 * <a href="https://github.com/JessYanCoding/MVPArms/wiki">See me</a>
 * <a href="https://github.com/JessYanCoding/MVPArmsTemplate">模版请保持更新</a>
 * ================================================
 */
public class
VdoListFragment extends BaseFragment<VdoListPresenter> implements VdoListContract.View , SwipeRefreshLayout.OnRefreshListener{

    String typeValue ;
    public VdoListFragment() {

    }



    @Override
    public void setupFragmentComponent(@NonNull AppComponent appComponent) {
        DaggerVdoListComponent //如找不到该类,请编译一下项目
                .builder()
                .appComponent(appComponent)
                .view(this)
                .build()
                .inject(this);
    }

    @Override
    public View initView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_vdo_list, container, false);
    }
    private V91PornAdapter mV91PornAdapter;

    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;
    @Override
    public void initData(@Nullable Bundle savedInstanceState) {
        Bundle bundle = getArguments();
        typeValue = bundle.getString("category");
        GridLayoutManager layoutManager = new GridLayoutManager(getContext(),2);
        recyclerView.setLayoutManager(layoutManager);
        recyclerView.setAdapter(mV91PornAdapter = new V91PornAdapter(R.layout.item_v_9porn , new ArrayList<>()));
        mSwipeRefreshLayout.setOnRefreshListener(this);
        mV91PornAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                Account account = mPresenter.getAccount();
                if (account != null){
                    int isVip = SharedPrefsUtils.getInt("isVip" , 1000);
                    if (isVip == 200){
                        Intent intent = new Intent(getActivity() , MyPlayVideoActivity.class);
                        intent.putExtra("bean" , ((ListBean)adapter.getData().get(position)));
                        getActivity().startActivity(intent);
                    }else {

                        long t1 = Long.parseLong(account.getVip())*1000 ;

                        long t2 = System.currentTimeMillis() ;

                        if ( t1 > t2){
                            Intent intent = new Intent(getActivity() , MyPlayVideoActivity.class);
                            intent.putExtra("bean" , ((ListBean)adapter.getData().get(position)));
                            getActivity().startActivity(intent);
                        }else {
                            showMemberDlg();
                        }
                    }

                } else {
                    EventBus.getDefault().post(new AddViewMsg("showLoginDlg",""));
                }
            }
        });
        mV91PornAdapter.setOnLoadMoreListener(new BaseQuickAdapter.RequestLoadMoreListener() {
            @Override
            public void onLoadMoreRequested() {
                mPresenter.loadVideoListData(typeValue  );
                mPresenter.loadCCListData(typeValue  );
            }
        }, recyclerView);

        mPresenter.typeSearch( typeValue  );

    }


    @Override
    public void setData(@Nullable Object data) {

    }
    @BindView(R.id.contentView)
    SwipeRefreshLayout mSwipeRefreshLayout;
    @Override
    public void showLoading(String key) {
        mSwipeRefreshLayout.setRefreshing(true);
    }

    @Override
    public void hideLoading() {
        mSwipeRefreshLayout.setRefreshing(false);
        mV91PornAdapter.loadMoreComplete();

    }

    @Override
    public boolean isCancel() {
        return false;
    }

    @Override
    public void showMessage(@NonNull String message) {
        checkNotNull(message);
        ArmsUtils.snackbarText(message);
    }

    @Override
    public void launchActivity(@NonNull Intent intent) {
        checkNotNull(intent);
        ArmsUtils.startActivity(intent);
    }

    @Override
    public void killMyself() {

    }



    @Override
    public void onRefresh() {
        mPresenter.typeSearch(typeValue );

    }

    @Override
    public void showTypeSearch(List<ListBean> list) {

//        mV91PornAdapter.setNewData(list);
        mPresenter.ccyySearch(typeValue  );
        mPresenter.hsck7Search(typeValue  );
    }

    @Override
    public void showLoadMore(List<ListBean> list) {
         mV91PornAdapter.addData(list);
        List<ListBean> xxx = LitePal.where("type=?",typeValue).limit(20).order("vod_time desc").find(ListBean.class);
        for (ListBean b :xxx) {
            b.setVod_time((int) (System.currentTimeMillis()/1000));
            b.delete();
            b.save();
        }
        mV91PornAdapter.addData(xxx);
    }

    @Override
    public void showCcyyHtml(List<ListBean> listBeans) {
        List<ListBean> xxx = LitePal.where("type=?",typeValue).limit(20).order("vod_time desc").find(ListBean.class);
        for (ListBean b :xxx) {
            b.setVod_time((int) (System.currentTimeMillis()/1000));
            b.delete();
            b.save();
        }
        mV91PornAdapter.addData(xxx);
    }


    public void showMemberDlg() {
        QMUIDialog.MessageDialogBuilder builder = new QMUIDialog.MessageDialogBuilder(getActivity());
        builder.setTitle("系统提示");
        builder.setMessage("您的会员已过期 ，请续费");
        builder.addAction("     卡密购买     ", (dialog, index) -> {

            String url = SharedPrefsUtils.getString("kamiUrl","");
            if (!TextUtils.isEmpty(url)){
                Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
                startActivity(intent);
            }else {
                showMessage("系统错误 ，请稍后在试");
            }

        });
        builder.addAction("     卡密充值     ", (dialog, index) -> {
            Intent intent = new Intent(getActivity() , MemberChargeActivity.class);
            startActivity(intent);

        });


        builder.show();



//        AlertDialog.Builder builder = new AlertDialog.Builder( this);
//        builder.setTitle("聚合直播");
//        builder.setMessage("您的会员已过期 ，请续费");
//        builder.setCancelable(true);            //点击对话框以外的区域是否让对话框消失
//
//        //设置正面按钮
//        builder.setPositiveButton("卡密购买", new DialogInterface.OnClickListener() {
//            @Override
//            public void onClick(DialogInterface dialog, int which) {
//                dialog.dismiss();
//                String url = "https://www.baidu.com/";
//                Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
//                startActivity(intent);
//                finish();
//            }
//        });
//        //设置反面按钮
//        builder.setNegativeButton("卡密充值", new DialogInterface.OnClickListener() {
//            @Override
//            public void onClick(DialogInterface dialog, int which) {
//                dialog.dismiss();
//                Intent intent = new Intent(getBaseContext() , MemberChargeActivity.class);
//                startActivity(intent);
//                finish();
//            }
//        });
        //设置中立按钮


//
//        AlertDialog dialog = builder.create();      //创建AlertDialog对象
//
//        builder.setCancelable(false);
//        builder.show();
//        dialog.show();
    }
}
